package com.yanqu.road.entity.activity.washe.game;

import com.yanqu.road.entity.activity.washe.data.WasheShowTime;
import com.yanqu.road.entity.activity.washe.eliminate.WasheGameData;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间数据
 */
public class WasheRoomData {

    // 基础数据
    private long roomId;
    private int modeId;
    private boolean begin = false;
    private long createTime = System.currentTimeMillis();
    private int appointSceneId = -1;

    // 玩家加入数据
    private WasheUserGameData player1 = null;
    private WasheUserGameData player2 = null;

    // 游戏开始后才有的数据
    private long beginTime;
    private int round;
    private int turnPlayer;                                         // 轮到谁了，1表示轮到player1
    private WasheGameData gameData;                                 // 盘面等数据

    // 表现时间
    private long expireTime;                                                // 超时时间
    private long showNo;                                                    // 最大表现编号
    private List<WasheShowTime> showTimeList = new ArrayList<>();           // 待表现数据
    private long showFinishTime;                                            // 预计动画播放完成时间（等于 超时时间 减 所有ShowTime）

    // 游戏结束数据
    private int winPlayer;                                          // 谁赢了，1表示player1赢了
    private int winType;                                            // 结束方式，WasheGameOverType

    private int peakWinPlayer;               // 巅峰赛新增，用于记录谁先赢了，我掉线了对方赢。对方掉线了我赢(掉线的情况下)

    private boolean isGuide;        // 是否新手引导

    // ================================================================

    public void addShowTime(long addTime) {
        showNo++;
        if (showTimeList.isEmpty()) {
            showFinishTime = System.currentTimeMillis();
        }
        showTimeList.add(new WasheShowTime(showNo, addTime));
        expireTime += addTime;
        showFinishTime += addTime;
    }

    // return true有提前结束，需要同步
    public boolean showFinish(long clientShowNo) {
        // 计算动画表现到这个no，理论所需时间
        long costTime = 0;
        for (int i = 0; i < 999; i++) {
            if (showTimeList.isEmpty()) {
                break;
            }
            WasheShowTime showTime = showTimeList.get(0);
            if (showTime.getShowNo() <= clientShowNo) {
                costTime += showTime.getTime();
                showTimeList.remove(0);
            } else {
                break;
            }
        }
        if (costTime <= 0) {
            return false;
        }

        // 计算动画表现到这个no，提前结束时间
        long tmpFinishTime = showFinishTime;
        for (WasheShowTime tmp : showTimeList) {
            tmpFinishTime -= tmp.getTime();
        }
        long now = System.currentTimeMillis();
        long subTime = tmpFinishTime - now;
        if (subTime <= 0) {
            return false;
        }

        // 修改时间
        expireTime -= subTime;
        showFinishTime -= subTime;
        return true;
    }

    public void addExpireTime(long addTime) {
        expireTime += addTime;
        showFinishTime += addTime;
    }

    // ================================================================

    public WasheGameData getGameData() {
        return gameData;
    }

    public void setGameData(WasheGameData gameData) {
        this.gameData = gameData;
    }

    public int getWinPlayer() {
        return winPlayer;
    }

    public void setWinPlayer(int winPlayer) {
        this.winPlayer = winPlayer;
    }

    public int getWinType() {
        return winType;
    }

    public void setWinType(int winType) {
        this.winType = winType;
    }

    public long getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(long beginTime) {
        this.beginTime = beginTime;
    }

    public boolean isBegin() {
        return begin;
    }

    public void setBegin(boolean begin) {
        this.begin = begin;
    }

    public long getRoomId() {
        return roomId;
    }

    public void setRoomId(long roomId) {
        this.roomId = roomId;
    }

    public int getModeId() {
        return modeId;
    }

    public void setModeId(int modeId) {
        this.modeId = modeId;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    public int getTurnPlayer() {
        return turnPlayer;
    }

    public void setTurnPlayer(int turnPlayer) {
        this.turnPlayer = turnPlayer;
    }

    public WasheUserGameData getPlayer1() {
        return player1;
    }

    public void setPlayer1(WasheUserGameData player1) {
        this.player1 = player1;
    }

    public WasheUserGameData getPlayer2() {
        return player2;
    }

    public void setPlayer2(WasheUserGameData player2) {
        this.player2 = player2;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public int getAppointSceneId() {
        return appointSceneId;
    }

    public void setAppointSceneId(int appointSceneId) {
        this.appointSceneId = appointSceneId;
    }

    // -------------- 这几个参数关联性很强，不要随便改 --------------

    public long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }

    public long getShowNo() {
        return showNo;
    }

    public void setShowNo(long showNo) {
        this.showNo = showNo;
    }

    public List<WasheShowTime> getShowTimeList() {
        return showTimeList;
    }

    public void setShowTimeList(List<WasheShowTime> showTimeList) {
        this.showTimeList = showTimeList;
    }

    public long getShowFinishTime() {
        return showFinishTime;
    }

    public void setShowFinishTime(long showFinishTime) {
        this.showFinishTime = showFinishTime;
    }

    public int getPeakWinPlayer() {
        return peakWinPlayer;
    }

    public void setPeakWinPlayer(int peakWinPlayer) {
        this.peakWinPlayer = peakWinPlayer;
    }

    public boolean isGuide() {
        return isGuide;
    }

    public void setGuide(boolean guide) {
        isGuide = guide;
    }
}
